//package se.peertv.client.nat.upnp;
// 
//import java.io.IOException;
//import java.net.InetAddress;
//import java.net.UnknownHostException;
//
//import net.sbbi.upnp.impls.InternetGatewayDevice;
//import net.sbbi.upnp.messages.ActionResponse;
//import net.sbbi.upnp.messages.UPNPResponseException;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import se.peertv.client.Main;
//import se.peertv.client.nat.ConnectivityInterface;
//
//public class UPnPInterface {
//
//	private static Logger log = LoggerFactory.getLogger(UPnPInterface.class);
//
//	public static enum Protocol {
//		UDP, TCP
//	}
//
//	public static int discoveryTimeout = 1500;
//
//	private static UPnPInterface upnpInt;
//
//	public static UPnPInterface getInstance() {
//		if (upnpInt == null) {
//			upnpInt = new UPnPInterface();
//			discoveryTimeout = Main.getMe().getClientConfig().getUPnPDiscoveryTime();
//		}
//		return upnpInt;
//	}
//
//	/**
//	 * Discovers all the root devices in the network that have a WAN interface.
//	 */
//	public InternetGatewayDevice[] discover() {
//		try {
//			InternetGatewayDevice[] IGDs = InternetGatewayDevice.getDevices(discoveryTimeout);
//			return IGDs;
//		} catch (Exception e) {
//			log.warn("UpnP discovery failed: " + e.getMessage());
//			return null;
//		}
//	}
//
//	public InetAddress getExternalAddress(InternetGatewayDevice internetGatewayDevice) {
//		try {
//			return InetAddress.getByName(internetGatewayDevice.getExternalIPAddress());
//		} catch (UnknownHostException e) {
//			return null;
//		} catch (UPNPResponseException e) {
//			return null;
//		} catch (Exception e) {
//			return null;
//		}
//	}
//
//	public boolean openPort(InternetGatewayDevice testIGD, int localPort, int publicPort, InetAddress myAddress, Protocol protocol) {
//		boolean mapped = false;
//		// System.out.println("NAT table size is " + testIGD.getNatTableSize());
//		// now let's open the port
//		String addressToMap = myAddress.getHostAddress();
//		try {
//
//			if (!ConnectivityInterface.boundable(localPort))
//				return false;
//
//			// localHostIP = InetAddress.getLocalHost().getHostAddress();
//			mapped = testIGD.addPortMapping("Some mapping description", null, localPort, publicPort, addressToMap, 0, protocol.name());
//			if (mapped) {
//				// checking on the device
//				ActionResponse resp = testIGD.getSpecificPortMappingEntry(null, publicPort, protocol.name());
//				if (resp != null) {
//					log.debug("External port " + publicPort + " successfully mapped to " + addressToMap + ":" + localPort);
//				}
//			}
//		} catch (UnknownHostException e) {
//			return false;
//		} catch (IOException e) {
//			return false;
//		} catch (UPNPResponseException e) {
//			return false;
//		}
//		return mapped;
//	}
//
//	public boolean removeMapping(InternetGatewayDevice testIGD, int externalPort, Protocol protocol) {
//		boolean unmapped = false;
//		try {
//			unmapped = testIGD.deletePortMapping(null, externalPort, protocol.name());
//			return unmapped;
//		} catch (IOException e) {
//			return false;
//		} catch (UPNPResponseException e) {
//			return false;
//		}
//
//	}
//
//}
